<template>
	<view class="selfAddress">
		<view class="headTop">
			<view class="title">地址簿</view>
			<navigator
				class="addressBtn"
				url="/pages/address/edit"
			>
				+ 添加地址
			</navigator>
		</view>
		<view class="list">
			<view
				class="item"
				v-for="(item, index) in addressList"
				:key="item._id"
				@longpress="goUpdate(item._id)"
				@click="selectAddress(item)"
			>
				<view class="head">
					<view class="user">{{ item.username }}, {{ item.mobile }}</view>
					<view class="select">
						<u-button
							class=""
							v-if="item.selected"
							plain
							color="#EC544F"
							type="primary"
							size="mini"
							text="默认地址"
						></u-button>
						<u-button
							class=""
							v-else
							plain
							color="#666"
							size="mini"
							text="设为默认"
							@click="setDefault(item._id)"
						></u-button>
					</view>
				</view>
				<view class="more">{{ item.area_name + item.address }}</view>
			</view>
		</view>
	</view>
</template>

<script>
const addressCloudObj = uniCloud.importObject('my-mall-address');
export default {
	data() {
		return {
			addressList: []
		};
	},
	onShow() {
		this.getAddressData();
	},
	methods: {
		async getAddressData() {
			let res = await addressCloudObj.getList();
			this.addressList = res.data;
		},
		// 设置默认地址
		async setDefault(id) {
			let res = await addressCloudObj.updateDefault(id);
			this.getAddressData();
		},
		//跳转到修改页面
		goUpdate(id) {
			uni.navigateTo({
				url: './edit?id=' + id
			});
		},
		// 用户选择地址
		selectAddress(item) {
			let { address, area_name, username, mobile } = item;
			let deliveryData = {
				address: area_name + address,
				username,
				mobile
			};

			uni.$emit('selectAddressEvent', deliveryData);
			uni.navigateBack();
		}
	}
};
</script>

<style lang="scss" scoped>
page {
	background-color: $page-bg-color;
	.selfAddress {
		padding: 30rpx;
		.headTop {
			@include flex-box();
			font-size: 30rpx;
			font-weight: bold;
			.addressBtn {
				border: 1px solid #000;
				border-radius: 50rpx;
				padding: 6rpx 15rpx;
			}
		}
		.list {
			padding-top: 30rpx;
			.item {
				background-color: #fff;
				padding: 40rpx 20rpx;
				margin-bottom: 30rpx;
				border-radius: 15rpx;
				.head {
					@include flex-box();
					font-weight: bold;
				}
				.more {
					font-size: 28rpx;
					color: $text-font-color-3;
					padding-top: 10rpx;
				}
			}
		}
	}
}
</style>
